<?php
	require_once("mysql.php");

	if($_POST["params"])
	{		
		$json_str = stripslashes($_POST["params"]);
		$data_in = json_decode($json_str, true);
		$sql = new SQLdb("common_user", "");

		if($data_in)
		{
			switch($data_in["type_query"])
			{
				case "SELECT":
					$qa = $sql->query_array("SELECT * FROM blog");
					$res["error"] = mysql_error();

					if($sql->res)
					{
						for($i = 0; $i < mysql_num_fields($sql->res); $i++)
						{
							$res["fields_data"][$i]["name"] = mysql_field_name($sql->res, $i);
							$res["fields_data"][$i]["type"] = mysql_field_type($sql->res, $i);
							$res["fields_data"][$i]["len"] = mysql_field_len($sql->res, $i);
							$res["fields_data"][$i]["flags"] = mysql_field_flags($sql->res, $i);
						}

						if($qa)
						{
							$i = 0;
							foreach($qa as $row)
							{	
								$j = 0;
								foreach($row as $field => $value)
								{	
									$res["rows_data"][$i][$j]["name"] = $field;
									$res["rows_data"][$i][$j]["value"] = $value;

									$j++;
								}
	
								$i++;
							}
						}
					}

					echo json_encode($res);
	
					break;

				case "INSERT":
					$qa = $sql->query_array("SELECT `id` FROM blog ORDER BY `id` DESC LIMIT 1");
					
					if($qa)
						$id = $qa[0]["id"] + 1;
					else
						$id = 0;

					$author = $data_in["data"][1];
					$title = $data_in["data"][2];
					$text = $data_in["data"][3];
				
					$sql->query("INSERT INTO blog (`id`, `author`, `title`, `text`) VALUES ('$id', '$author', '$title', '$text')");
					$res["error"] = mysql_error();

					$qa = $sql->query_array("SELECT * FROM blog WHERE `id` = '$id'");
					
					if($sql->res)
					{
						if($qa && $qa[0])
						{
							$j = 0;
							foreach($qa[0] as $field => $value)
							{	
								$res["rows_data"][$j]["name"] = $field;
								$res["rows_data"][$j]["value"] = $value;

								$j++;
							}

						}
					}

					$res["type"] = "update";

					echo json_encode($res);
					break;

				case "UPDATE":
					$id = $data_in["data"]["select"][0]["value"];

					$updates = "";
					foreach($data_in["data"]["update"] as $upd)
					{
						if($i != 0)
							$updates .= ", ";

						$updates .= "`{$upd["name"]}` = '{$upd["value"]}'";
						$i++;
					}

					$sql->query("UPDATE blog SET $updates WHERE `id` = $id");
					$res["error"] = mysql_error();

					$qa = $sql->query_array("SELECT * FROM blog WHERE `id` = '$id'");
					
					if($sql->res)
					{
						if($qa && $qa[0])
						{
							$j = 0;
							foreach($qa[0] as $field => $value)
							{	
								$res["rows_data"][$j]["name"] = $field;
								$res["rows_data"][$j]["value"] = $value;

								$j++;
							}

						}
					}

					$res["type"] = "update";

					echo json_encode($res);
	
					
					break;
					

				case "DELETE":
					$sql->query("DELETE FROM blog WHERE `id` = '" . $data_in["data"][0]["value"] . "'");
					$res["error"] = mysql_error();

					$res["type"] = "delete";

					echo json_encode($res);
				break;
					
			}
		}
	}

?>
